package com.ruoyi.api.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DzTiku;
import com.ruoyi.system.service.IDzTikuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题库信息Controller
 * 
 * @author ruoyi
 * @date 2022-09-02
 */
@RestController
@RequestMapping("/api/tiku")
public class DzTikuApiController extends BaseController
{
    @Autowired
    private IDzTikuService dzTikuService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询题库信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DzTiku dzTiku)
    {
        startPage();
        List<DzTiku> list = dzTikuService.selectDzShiList(dzTiku);
        return getDataTable(list);
    }


    @GetMapping("/cuolist")
    public TableDataInfo cuolist(DzTiku dzTiku)
    {
        startPage();
        List<DzTiku> list = dzTikuService.selectDzCuoList(dzTiku);
        return getDataTable(list);
    }


    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }


    /**
     * 导出题库信息列表
     */
    @Log(title = "题库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DzTiku dzTiku)
    {
        List<DzTiku> list = dzTikuService.selectDzTikuListWeb(dzTiku);
        ExcelUtil<DzTiku> util = new ExcelUtil<DzTiku>(DzTiku.class);
        util.exportExcel(response, list, "题库信息数据");
    }

    /**
     * 获取题库信息详细信息
     */
    @GetMapping(value = "/{tiId}")
    public AjaxResult getInfo(@PathVariable("tiId") Long tiId)
    {
        return AjaxResult.success(dzTikuService.selectDzTikuByTiId(tiId));
    }

    /**
     * 新增题库信息
     */
    @Log(title = "题库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DzTiku dzTiku)
    {
        return toAjax(dzTikuService.insertDzTiku(dzTiku));
    }

    /**
     * 修改题库信息
     */
    @Log(title = "题库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DzTiku dzTiku)
    {
        return toAjax(dzTikuService.updateDzTiku(dzTiku));
    }

    /**
     * 删除题库信息
     */
    @Log(title = "题库信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tiIds}")
    public AjaxResult remove(@PathVariable Long[] tiIds)
    {
        return toAjax(dzTikuService.deleteDzTikuByTiIds(tiIds));
    }
}
